package com.hns.can.safety.web;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.web.BaseController;
import com.hns.can.safety.entity.CommCarSpdingEntity;
import com.hns.can.safety.service.OverspeedService;
import com.hns.can.safety.vo.OverspeedMonitorParamVo;
import com.hns.can.safety.vo.OverspeedStatisticsParamVo;
import com.hns.can.safety.vo.SpedReportSummaryVo;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.DateTool;

/**
 * @版权：HNS 版权所有 (c) 2013
 * @author:JeffXu
 * @version Revision 1.0.0
 * @email:835264047@qq.com
 * @see:
 * @创建日期：2014-7-9
 * @功能说明：超速统计控制类
 * @begin
 * @修改记录:
 * @修改后版本          修改人      	修改内容
 * @2014-7-9  	         JeffXu        	创建
 * @end
 */
@Controller
@RequestMapping("/overspeed")
public class OverspeedController extends BaseController {
    
    @Autowired
    private OverspeedService overspeedService; 
    
    final private String[] headerNames = {"所属机构", "运营线路", "运营日期", "超速车台(台)","超速次数(次)","严重次数(次)", "一般次数(次)", "警告次数(次)", "其他警告次数(次)" };
    final private String[] headerFields = {"organName", "lineName", "operDate", "spedCarCnt", "spedCnt","seriousCnt","commonCnt", "warningCnt", "otherWarningCnt"};
   
    final private String[] headerNames2 =  {"驾驶员", "车牌号", "车辆编号", "营运线路","所属机构","超速开始值(KM/H)","超速平均值(KM/H)","超速期间速度最大值(KM/H)","限速值(KM/H)","超速百分比","严重度","超速时长","发生时间","发生地点","详细地址"};
    final private String[] headerFields2 = {"drvName", "licPltNo", "carInCd", "lineName", "organName","speedBeginValue","spdingPrdSpeedAvrgValue","spdingPrdSpeedMaxVal", "spdingLitVal", "spdingPercent","spdingSeverity", "spdingTimeLong", "rcrdTime", "rcrdPlace", "detailAddr"};

    
    /**
     * 得到超速统计数据
     * @param conditionVo
     * @param request
     * @param response
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/searchOverspeedStatisticsData")
    @ResponseBody
    public Object searchOverspeedStatisticsData(OverspeedStatisticsParamVo conditionVo, HttpServletResponse response) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        conditionVo.setCorpId(user.getCorpId());
        conditionVo.setDriverRelTableName("T_COMM_CAR_SPDING");
        Pagination page = overspeedService.searchOverspeedStatisticsData(conditionVo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * 得到超速统计数据List
     * @param conditionVo
     * @param request
     * @param response
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/searchOverspeedStatisticsList")
    @ResponseBody
    public Object searchOverspeedStatisticsList(OverspeedStatisticsParamVo conditionVo, HttpServletResponse response) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        conditionVo.setCorpId(user.getCorpId());
        conditionVo.setDriverRelTableName("T_COMM_CAR_SPDING");
        List<SpedReportSummaryVo> list = overspeedService.searchOverspeedStatisticsList(conditionVo);
        return this.getReturnResult(list);
    }
    
    /**
     * 导出超速统计excel
     * @param conditionVo
     * @param request
     * @param response
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/exportStatisExcel")
    public void exportStatisExcel(OverspeedStatisticsParamVo conditionVo,HttpServletResponse response) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        conditionVo.setCorpId(user.getCorpId());
        conditionVo.setDriverRelTableName("T_COMM_CAR_SPDING");
        List<SpedReportSummaryVo> list = overspeedService.searchOverspeedStatisticsList(conditionVo);
        ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, response, "excel", URLEncoder.encode("超速统计", "UTF-8") + DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "超速统计");
    }
    
    /**
     * 导出超速统计pdf
     * @param conditionVo
     * @param request
     * @param response
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/exportStatisPdf")
    public void exportStatisPdf(OverspeedStatisticsParamVo conditionVo,HttpServletResponse response) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        conditionVo.setCorpId(user.getCorpId());
        conditionVo.setDriverRelTableName("T_COMM_CAR_SPDING");
        List<SpedReportSummaryVo> list = overspeedService.searchOverspeedStatisticsList(conditionVo);
        ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, response, "pdf", URLEncoder.encode("超速统计", "UTF-8") + DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "超速统计");
    }
    
    /**
     * @param conditionVo
     * @param request
     * @param response
     * @throws Exception
     * @author:JeffXu
     * @email:835264047@qq.com
     * @创建日期:2014-7-11
     * @功能说明：得到超速信息表
     */
    @RequestMapping("/searchOverspeedPage")
    @ResponseBody
    public Object searchOverspeedPage(OverspeedMonitorParamVo conditionVo) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        conditionVo.setCorpId(user.getCorpId());
        conditionVo.setDriverRelTableName("T_COMM_CAR_SPDING");
        Pagination page = overspeedService.searchOverspeedPage(conditionVo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * @param conditionVo
     * @param request
     * @param response
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     * @创建日期:2014-8-15
     * @功能说明：查询当天 报警灯超速信息
     */
    /*@SuppressWarnings("unchecked")
    @RequestMapping("/searchOverspeedPageWarnLight")
    public void searchOverspeedPageWarnWarnLight(OverspeedMonitorParamVo conditionVo, HttpServletRequest request, 
                HttpServletResponse response, Map<String, Object> map) throws Exception {
        
        conditionVo.setBeginTime(DateTool.SHORT_DATE_FORMAT.format(new Date()));
        conditionVo.setEndTime(DateTool.SHORT_DATE_FORMAT.format(new Date()));
        List<String> orgIdList = (List<String>)map.get("SES_ORGANLIST");
        List<String> carList = baseCarInfoService.getBaseCarInfoListByOrgIdList(orgIdList);
        
        conditionVo.setType(LeftOrgNodeType.CAR.getCode());
        conditionVo.setOrgIds(carList);
        SysUserEntity user = (SysUserEntity)map.get("SES_CURRENTUSER");
        conditionVo.setCorpId(user.getCorpId());
        conditionVo.setDriverRelTableName("T_COMM_CAR_SPDING");
        Pagination page = overspeedMonitorService.searchOverspeedPage(conditionVo,request);
        GridPage grid = new GridPage(page);
        ResponseUtil.responseJSON(grid, response);
    }*/
    
    /**
     * 导出超速明细excel
     * @param conditionVo
     * @param request
     * @param response
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/exportOverspeedExcel")
    @ResponseBody
    public void exportOverspeedExcel(OverspeedMonitorParamVo conditionVo,HttpServletResponse response) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        conditionVo.setCorpId(user.getCorpId());
        conditionVo.setDriverRelTableName("T_COMM_CAR_SPDING");
        List<CommCarSpdingEntity> list = overspeedService.getOverspeedList(conditionVo);
        ExportUtil.exportFiles(Arrays.asList(headerNames2), Arrays.asList(headerFields2), list, response, "excel", URLEncoder.encode("超速明细", "UTF-8") + DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "超速明细");
    }
    
    /**
     * 导出超速明细pdf
     * @param conditionVo
     * @param response
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/exportOverspeedPdf")
    public void exportOverspeedPdf(OverspeedMonitorParamVo conditionVo, HttpServletResponse response) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        conditionVo.setCorpId(user.getCorpId());
        conditionVo.setDriverRelTableName("T_COMM_CAR_SPDING");
        List<CommCarSpdingEntity> list = overspeedService.getOverspeedList(conditionVo);
        ExportUtil.exportFiles(Arrays.asList(headerNames2), Arrays.asList(headerFields2), list, response, "pdf", URLEncoder.encode("超速明细", "UTF-8") + DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "超速明细");
    }
    
//    /**
//     * 得到超速统计数据
//     * @param conditionVo
//     * @param request
//     * @param response
//     * @return
//     * @throws Exception
//     * @author:lan
//     * @email:763836737@qq.com
//     */
//    @RequestMapping("/searchOverspeedStatisticsData")
//    @ResponseBody
//    public Object searchOverspeedStatisticsData(OverspeedStatisticsParamVo conditionVo, HttpServletResponse response) throws Exception {
//        SysUserEntity user = this.getCurrentUser().getUser();
//        conditionVo.setCorpId(user.getCorpId());
//        conditionVo.setDriverRelTableName("T_COMM_CAR_SPDING");
//        Pagination page = overspeedService.searchOverspeedStatisticsData(conditionVo);
//        GridPage grid = new GridPage(page);
//        return this.getReturnResult(grid);
//    }
}
